package cn.tworice.admin.utils;

import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class RestTemplateOfCustom {
    public static String sendPost(String url ,String accessToken,String content){
        // 创建一个HttpHeaders对象，并设置内容类型为application/json
        org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);

        // 将accessToken添加到请求头中
//        headers.set("Authorization", "Bearer " + accessToken);

        // 将请求体内容封装为HttpEntity
        org.springframework.http.HttpEntity<String> requestEntity = new org.springframework.http.HttpEntity<>(content, headers);
        RestTemplate restTemplate = new RestTemplate();
        // 发送POST请求并接收响应
        org.springframework.http.ResponseEntity<String> responseEntity = restTemplate.exchange(
                url+"",
                org.springframework.http.HttpMethod.POST,
                requestEntity,
                String.class
        );
        String body = responseEntity.getBody();
        System.out.println(body);
        // 获取响应体内容
        return responseEntity.getBody().toString();
    }


}
